package com.itcrowd.ittao.view;

import com.itcrowd.ittao.R;
import com.itcrowd.ittao.service.ShoppingListManager;
import com.itcrowd.ittao.service.data.Product;

import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class OrderListActivity extends FragmentActivity implements OnItemClickListener {

	private OrderListAdapter mAdapter;
	
	public void onCreate(Bundle savedInstance) {
		super.onCreate(savedInstance);
		setContentView(R.layout.orderlist);
		int title = Resources.getSystem().getIdentifier("action_bar_title", "id", "android");
		TextView titleView = (TextView) findViewById(title);
		titleView.setTextColor(Color.WHITE);

		getActionBar().setDisplayHomeAsUpEnabled(true);
		
		ListView list = (ListView) findViewById(R.id.list);
		mAdapter = new OrderListAdapter();
		list.setAdapter(mAdapter);
		list.setEmptyView(findViewById(R.id.empty_view));
		list.setOnItemClickListener(this);
	}
	
	public void onResume() {
		super.onResume();
		mAdapter.notifyDataSetChanged();
	}
	
	public class OrderListAdapter extends BaseAdapter {

		private ShoppingListManager mShoppingListManager;
		private LayoutInflater mInflater;
		
		public OrderListAdapter() {
			mShoppingListManager = ShoppingListManager.getInstance();
			mInflater = LayoutInflater.from(OrderListActivity.this);
		}
		
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mShoppingListManager.getOrderList().size();
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return mShoppingListManager.getOrderList().get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup arg2) {
			if(convertView == null) {
				convertView = mInflater.inflate(android.R.layout.simple_list_item_2, null, false);
				((TextView) convertView.findViewById(android.R.id.text2)).setTextColor(Color.RED);
			}
			
			Product product = (Product) getItem(position);
			convertView.setTag(product);
			TextView text = (TextView) convertView.findViewById(android.R.id.text1);
			text.setText(product.getName());
			
			TextView text2 = (TextView) convertView.findViewById(android.R.id.text2);
			String productStatus = getString(R.string.product_cost_status); 
			productStatus = String.format(productStatus, product.getProductStatus().getCurrentNumber(), product.getCost().toString());
			text2.setText(productStatus);
			return convertView;
		}
		
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		Product product = (Product) arg1.getTag();
		Intent intent = new Intent(this, ProductDetailActivity.class);
		intent.putExtra("product", product.getQRCode());
		startActivity(intent);
	}
	
	public boolean onOptionsItemSelected(MenuItem item) {
		if(item.getItemId() == android.R.id.home) {
			finish();
		}
		return true;
	}
}
